<?
if (!defined('SMF'))
	die('Hacking attempt...');

include_once("xenobe_funcs.php");

global $targetlink, $xenobeisdead, $xenobe_max, $xen_aggression;
global $start_turns, $start_credits, $max_planets_sector;
global $fed_sectors, $sector_max, $xen_planets;

$xencount = $xencount0 = $xencount0a = $xencount1 = $xencount1a = $xencount2 = $xencount2a = $xencount3 = $xencount3a = $xencount3h = 0;

// Find all xenobes that are alive
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	LEFT JOIN {raw:db_xenobes} ON {raw:db_xenobes}.ship_id = {raw:db_ships}.ship_id
	WHERE {raw:db_ships}.xenobe = "Y"
	AND ship_destroyed = "N"
	ORDER BY sector',
	array(
		'db_ships' => $dbtables['ships'],
		'db_xenobes' => $dbtables['xenobes'],
	)
);
  
while ($xenobeinfo = $smcFunc['db_fetch_assoc']($result))
{
    $xenobeisdead = false;
	
    // Regenerate defenses
    xenoberegen($xenobeinfo);
	
    // Update total xenobe count, used to regenerate xenobe later if neccassary.
    $xencount++;
	
	// 20% chance of not moving at all 
    if (rand(1,5) > 1)
    {
		// Orders 0, Sentinel
		if ($xenobeinfo['orders'] == 0)
		{
			$xencount0++;
			
			// Xenobe looks for a target in the CURRENT sector, not a xenobe, not on a planet.
			$target_result = $smcFunc['db_query']('', '
				SELECT *
				FROM {raw:db_ships}
				WHERE sector = {int:sector}
				AND xenobe = "N"
				AND on_planet = "N"
				LIMIT 1',
				array(
					'db_ships' => $dbtables['ships'],
					'sector' => $xenobeinfo['sector'],
				)
			);
			
			if ($smcFunc['db_num_rows']($target_result) <> 0)
			{
				$target = $smcFunc['db_fetch_assoc']($target_result);
				$smcFunc['db_free_result']($target_result);
				
				// Orders 0, Agression 0, Peaceful
				if ($xenobeinfo['aggression'] == 0)            
				{
					// Do Nothing
				}
				// Orders 0, Agression 1, Attack Sometimes
				elseif ($xenobeinfo['aggression'] == 1)        
				{
					// Xenobes only compare number of fighters when determining if they have an attack advantage
					if ($xenobeinfo['ship_fighters'] > $target['ship_fighters'])
					{
						$xencount0a++;
						playerlog($xenobeinfo['ship_id'], LOG_XENOBE_ATTACK, "$target[character_name]");
						$xenoboeisdead = xenobetoship($xenobeinfo,$target['ship_id']);
						if ($xenobeisdead)
							continue;
					}
				}
				// Orders 0, Agression 2, Attack Always
				elseif ($xenobeinfo['aggression'] == 2)        
				{
					$xencount0a++;
					playerlog($xenobeinfo['ship_id'], LOG_XENOBE_ATTACK, "$target[character_name]");
					$xenoboeisdead = xenobetoship($xenobeinfo,$target['ship_id']);
					if ($xenobeisdead)
						continue;
				}
			}
		}
		// Orders 1, Roam
		elseif ($xenobeinfo['orders'] == 1)
		{
			$xencount1++;
			
			// Roam to a new sector before doing anything else
			$xenobeisdead = xenobemove($xenobeinfo,$xenobeinfo['sector']);
			if ($xenobeisdead)
				continue;
			
			// Update xenobeinfo post move
			$xenobeinfo_result = $smcFunc['db_query']('', '
				SELECT *
				FROM {raw:db_ships}
				LEFT JOIN {raw:db_xenobes} ON {raw:db_xenobes}.ship_id = {raw:db_ships}.ship_id
				WHERE {raw:db_ships}.ship_id = {int:ship_id}
				ORDER BY sector',
				array(
					'db_ships' => $dbtables['ships'],
					'db_xenobes' => $dbtables['xenobes'],
					'ship_id' => $xenobeinfo['ship_id']
				)
			);
			$xenobeinfo = $smcFunc['db_fetch_assoc']($xenobeinfo_result);
			$smcFunc['db_free_result']($xenobeinfo_result);
			
			// Find a target, in the current sector, not a xenobe
			$target_result = $smcFunc['db_query']('', '
				SELECT *
				FROM {raw:db_ships}
				WHERE sector = {int:sector}
				AND xenobe = "N"
				LIMIT 1',
				array(
					'db_ships' => $dbtables['ships'],
					'sector' => $xenobeinfo['sector'],
				)
			);
			
			if ($smcFunc['db_num_rows']($target_result) <> 0)
			{
				$target = $smcFunc['db_fetch_assoc']($target_result);
				$smcFunc['db_free_result']($target_result);
				
				// Orders 1, Agression 0, Peaceful
				if ($xenobeinfo['aggression'] == 0)
				{
					// Do Nothing
				}
				// Orders 1, Agression 1, Attack Sometimes
				elseif ($xenobeinfo['aggression'] == 1)
				{
					// Xenobes only compare number of fighters when determining if they have an attack advantage
					// This xenobe will not attack players parked on planets.
					if ($xenobeinfo['ship_fighters'] > $target['ship_fighters'] && $target['on_planet'] == 'N')
					{
						$xencount1a++;
						playerlog($xenobeinfo['ship_id'], LOG_XENOBE_ATTACK, "$target[character_name]");
						$xenoboeisdead = xenobetoship($xenobeinfo,$target['ship_id']);
						if ($xenobeisdead)
							continue;
					}
				}
				// Orders 1, Agression 2, Attack Always
				elseif ($xenobeinfo['aggression'] == 2)
				{
					$xencount1a++;
					playerlog($xenobeinfo['ship_id'], LOG_XENOBE_ATTACK, "$target[character_name]");
					
					// Check if target is on a planet
					if ($target['on_planet'] == 'Y')
						$xenoboeisdead = xenobetoplanet($xenobeinfo,$target['planet_id']);
					else
						$xenoboeisdead = xenobetoship($xenobeinfo,$target['ship_id']);
						
					if ($xenobeisdead)
						continue;
				}
			}
		}
		// Orders 2, Roam and Trade
		elseif ($xenobeinfo['orders'] == 2)
		{
			$xencount2++;
			
			// Roam to a new sector before doing anything else
			$xenobeisdead = xenobemove($xenobeinfo,$xenobeinfo['sector']);
			if ($xenobeisdead)
				continue;
			
			// Update xenobeinfo post move
			$xenobeinfo_result = $smcFunc['db_query']('', '
				SELECT *
				FROM {raw:db_ships}
				LEFT JOIN {raw:db_xenobes} ON {raw:db_xenobes}.ship_id = {raw:db_ships}.ship_id
				WHERE {raw:db_ships}.ship_id = {int:ship_id}
				ORDER BY sector',
				array(
					'db_ships' => $dbtables['ships'],
					'db_xenobes' => $dbtables['xenobes'],
					'ship_id' => $xenobeinfo['ship_id']
				)
			);
			$xenobeinfo = $smcFunc['db_fetch_assoc']($xenobeinfo_result);
			$smcFunc['db_free_result']($xenobeinfo_result);
			
			// Trade before any aggression checks
			xenobetrade($xenobeinfo);
			
			// Find a target, in the current sector, not a xenobe
			$target_result = $smcFunc['db_query']('', '
				SELECT *
				FROM {raw:db_ships}
				WHERE sector = {int:sector}
				AND xenobe = "N"
				LIMIT 1',
				array(
					'db_ships' => $dbtables['ships'],
					'sector' => $xenobeinfo['sector'],
				)
			);
			
			if ($smcFunc['db_num_rows']($target_result) <> 0)
			{
				$target = $smcFunc['db_fetch_assoc']($target_result);
				$smcFunc['db_free_result']($target_result);
				
				// Orders 2, Agression 0, Peaceful
				if ($xenobeinfo['aggression'] == 0)
				{
					// Do Nothing
				}
				// Orders 2, Agression 1, Attack Sometimes
				elseif ($xenobeinfo['aggression'] == 1)
				{
					// Xenobes only compare number of fighters when determining if they have an attack advantage
					// This xenobe will not attack players parked on planets.
					if ($xenobeinfo['ship_fighters'] > $target['ship_fighters'] && $target['on_planet'] == 'N')
					{
						$xencount2++;
						playerlog($xenobeinfo['ship_id'], LOG_XENOBE_ATTACK, "$target[character_name]");
						$xenoboeisdead = xenobetoship($xenobeinfo,$target['ship_id']);
						if ($xenobeisdead)
							continue;
					}
				}
				// Orders 2, Agression 2, Attack Always
				elseif ($xenobeinfo['aggression'] == 2)
				{
					$xencount2++;
					playerlog($xenobeinfo['ship_id'], LOG_XENOBE_ATTACK, "$target[character_name]");
					
					// Check if target is on a planet
					if ($target['on_planet'] == 'Y')
						$xenoboeisdead = xenobetoplanet($xenobeinfo,$target['planet_id']);
					else
						$xenoboeisdead = xenobetoship($xenobeinfo,$target['ship_id']);
						
					if ($xenobeisdead)
						continue;
				}
			}
		}
		// Orders 3, Roam and Hunt
		elseif ($xenobeinfo['orders'] == 3)
		{
			$xencount3++;
			
			// Order 3 xenobes have a 25% chance to "go hunting"
			$hunt = rand(0,3);
			
			if ($hunt == 0)
			{
				$xencount3h++;
				$xenobeisdead = xenobehunter($xenobeinfo);
				if ($xenobeisdead)
					continue;
			}
			else
			{
				// Roam to a new sector before doing anything else
				$xenobeisdead = xenobemove($xenobeinfo,$xenobeinfo['sector']);
				if ($xenobeisdead)
					continue;
					
				// Update xenobeinfo post move
				$xenobeinfo_result = $smcFunc['db_query']('', '
					SELECT *
					FROM {raw:db_ships}
					LEFT JOIN {raw:db_xenobes} ON {raw:db_xenobes}.ship_id = {raw:db_ships}.ship_id
					WHERE {raw:db_ships}.ship_id = {int:ship_id}
					ORDER BY sector',
					array(
						'db_ships' => $dbtables['ships'],
						'db_xenobes' => $dbtables['xenobes'],
						'ship_id' => $xenobeinfo['ship_id']
					)
				);
				$xenobeinfo = $smcFunc['db_fetch_assoc']($xenobeinfo_result);
				$smcFunc['db_free_result']($xenobeinfo_result);
					
				// Find a target, in the current sector, not a xenobe
				$target_result = $smcFunc['db_query']('', '
					SELECT *
					FROM {raw:db_ships}
					WHERE sector = {int:sector}
					AND xenobe = "N"
					LIMIT 1',
					array(
						'db_ships' => $dbtables['ships'],
						'sector' => $xenobeinfo['sector'],
					)
				);
				
				if ($smcFunc['db_num_rows']($target_result) <> 0)
				{
					$target = $smcFunc['db_fetch_assoc']($target_result);
					$smcFunc['db_free_result']($target_result);
					
					// Orders 3, Agression 0, Peaceful
					if ($xenobeinfo['aggression'] == 0)
					{
						// Do Nothing
					}
					// Orders 3, Agression 1, Attack Sometimes
					elseif ($xenobeinfo['aggression'] == 1)
					{
						// Xenobes only compare number of fighters when determining if they have an attack advantage
						// This xenobe will not attack players parked on planets.
						if ($xenobeinfo['ship_fighters'] > $target['ship_fighters'] && $target['on_planet'] == 'N')
						{
							$xencount3h++;
							playerlog($xenobeinfo['ship_id'], LOG_XENOBE_ATTACK, "$target[character_name]");
							$xenoboeisdead = xenobetoship($xenobeinfo,$target['ship_id']);
							if ($xenobeisdead)
								continue;
						}
					}
					// Orders 3, Agression 2, Attack Always
					elseif ($xenobeinfo['aggression'] == 2)
					{
						$xencount3h++;
						playerlog($xenobeinfo['ship_id'], LOG_XENOBE_ATTACK, "$target[character_name]");
						
						// Check if target is on a planet
						if ($target['on_planet'] == 'Y')
							$xenoboeisdead = xenobetoplanet($xenobeinfo,$target['planet_id']);
						else
							$xenoboeisdead = xenobetoship($xenobeinfo,$target['ship_id']);
							
						if ($xenobeisdead)
							continue;
					}
				}
			}
		}
    }
}

$smcFunc['db_free_result']($result);

/*	
	Xenobe Auto Generator
	Spawns new Xenobe each update to keep the amount of Xenobe in the universe at $xenobe_max.
	New xenobe will be set to the average player hull + or - 7
*/
	
$needed_xenobe = $xenobe_max - $xencount;
if ($needed_xenobe >= 0)
{
    $message .= '<p>Creating ' . $needed_xenobe . ' Xenobe.</p>';
    
	$result = $smcFunc['db_query']('', '
		SELECT round(AVG(hull)) AS hull, round(AVG(power)) AS power, round(AVG(computer)) AS computer, COUNT(*) AS count
		FROM {raw:db_ships}
		WHERE ship_destroyed = "N"
		AND xenobe = "N"',
		array(
			'db_ships' => $dbtables['ships'],
		)
	);
	$row = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
    $playeraverage = round(($row['hull'] + $row['power']  + $row['computer']) / 3);
	
	$message .= $row['count'] . ' Xenobes used for an average of ' . $playeraverage . '</p>'; 

    while ($needed_xenobe > 0)
    {
        $xenlevel = $playeraverage + rand(-2,4);
	 	
		// Make sure no negative levels are used
		if ($xenlevel <= 0)
			$xenlevel = rand(1,$xenstartsize);
	 	
		// New code to limit xenobe size, stops undeafetable xenobe being created - GunSlinger
		if ($xenlevel > 52)
		{
			$xenlevel = 52;
			$message .= '<p>Lowering Xenobe average too big !</p>';
		}
		
		/*
			Making cloak half the size of the xenobe level so users see more of them.
			I know this affects other calcs, but not against players - rjordan
		*/
		$xen_cloak = round($xenlevel / 2);
		$fpf = (int) $xenlevel * 1000000;
		$message .= '<p>Creating level ' . $xenlevel . ' Xenobe</p>'; 	 
	 			 	 
		// Create Xenobe Name
        $Syllable2 = array("Ak","Al","Ar","B","Br","D","F","Fr","G","Gr","H","J","N","Ol","Om","P","Qu","R","S","T","Z");
        $Syllable3 = array("a","ar","aka","aza","e","el","i","in","int","ili","ish","ido","ir","o","oi","or","os","ov","otre","u","un");
        $Syllable4 = array("ag","al","ak","ba","dar","g","ga","k","ka","kar","kil","l","n","ns","ol","r","s","st","ta","ti","x");
        $Syllable5 = array("an","yen","avi","aka","ens","elf","si","pin","rint","eli","ash","ids","sir","os","soi","por","ost","rov","more","urn","onk");

		/*
			New code for shipnames orig code from gcfl.net
			Use the 1st $ShipNames below for the 1st update after universe creation by commenting out the 2nd $ShipNames line
			Then after update uncomment the 2nd one and comment out the 1st $ShipNames line. - GunSlinger
		*/

        $ShipNames = array("Scount", "Steamrunner", "Battle Cruiser", "Death Glider", "Starship", "Deathstar", "Excelsior", "Armageddon", "Copernicus", "Crazy Horse", "Daedalus", "Endeavour", "Equinox", "Fearless", "Nebula", "Intrepid", "Warship", "Odyssey", "Pegasus", "Prometheus", "Brave Star", "Pueblo", "Renegade", "Spaceship", "Stargazer", "Amazon Queen", "Destructor", "Andromeda", "Devastator",  "Event Horizon", "Storm Force", "Reaper", "Mort", "Repulsifier", "Erradicator", "Annihilator", "Tattoo", "Anthrax", "Scylla", "Alacrity", "Red Alert", "Dog Star", "Invincible", "Predator", "Bird of Prey", "Slip Stream", "Devils Horn", "Crusader", "Explorer", "Shadow", "Ulysses", "Punisher", "Iron Maiden", "Thors Hammer", "Thors Chariot", "Solar Storm", "Raptor", "Speed Demon", "Cubix", "Talisman", "Sonic Boom", "Vampyrrhic", "Zed Bed", "Evolution", "Sensai", "Nostradamus", "Harvester", "Sol Slip", "Comet", "Aurora", "Oddysee", "Hive", "Adversary", "Zeppellin", "Zephyr");

        $sy2roll = rand(0,20);
        $sy3roll = rand(0,20);
        $sy4roll = rand(0,20);
        $sy5roll = rand(0,20);
        $character = $Syllable2[$sy2roll] . $Syllable3[$sy3roll] . $Syllable4[$sy4roll] . $Syllable5[$sy5roll];
		
		$result = $smcFunc['db_query']('', '
			SELECT character_name
			FROM {raw:db_ships}
			WHERE character_name = {string:character}
			LIMIT 1',
			array(
				'db_ships' => $dbtables['ships'],
				'character' => $character,
			)
		);
        $namecheck = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);

		// If Name Exists Try Again
		$i = 0;
		$namecheck = '';
		while (empty($namecheck) && $i < 100)
		{
			$sy2roll = rand(0,20);
			$sy3roll = rand(0,20);
			$sy4roll = rand(0,20);
			$sy5roll = rand(0,20);
			$character = $Syllable2[$sy2roll] . $Syllable3[$sy3roll] . $Syllable4[$sy4roll] . $Syllable5[$sy5roll];
			
			$result = $smcFunc['db_query']('', '
				SELECT character_name
				FROM {raw:db_ships}
				WHERE character_name = {string:character}
				LIMIT 1',
				array(
					'db_ships' => $dbtables['ships'],
					'character' => $character,
				)
			);
			
			if ($smcFunc['db_num_rows']($result) > 0)
			{
				$namecheck = $smcFunc['db_fetch_assoc']($result);
				$smcFunc['db_free_result']($result);
			}

			$i++;
        }
		
		// Create Ship Name
        $shipname = $ShipNames[rand(0,count($ShipNames) - 1)];
		
		// Select Random Sector
        $sector = rand($fed_sectors,$sector_max - 1);
		
		/*
			Select orders as follows:
			
			0 = Sentinel
			1 = Explorer
			2 = Trader
			3 = Hunter
			
			If you dont want hunters set it to (1,2), if you want sentinels then go (0,2), etc
		*/
		$orders = rand(0,2);
		
		// Select aggression as follows:
		$aggression = rand(1,100);
		
		if ($aggression <= $xen_aggression)
			// I do this to create more peacefull than aggressive.
			// This creates 33% aggressive.  I will make configurable later - Jordo // originally 1 GunSlinger
			$aggression = rand(1,2);
		else
			$aggression = 0;
		
		// Add xenobe record to ships table
		$active = 'Y';
		$errflag = 0;
		
		if ($character == '' || $shipname == '')
		{
			$message .= '<p>Ship name and character name may not be blank.</p>';
			$errflag = 1;
		}
			
		if ($errflag == 0)
		{
			$maxenergy = NUM_ENERGY($xenlevel);
			$maxarmor = NUM_ARMOUR($xenlevel);
			$maxfighters = NUM_FIGHTERS($xenlevel);
			$maxtorps = NUM_TORPEDOES($xenlevel);
			
			// Make all new xenobe's somewhat evil
			$init_rating = 0 - (rand(1,1000));

			// Insert the new xenobe into the ships database
			$smcFunc['db_insert']('insert',
				$dbtables['ships'],
				array(
					'ship_name' => 'string',
					'ship_destroyed' => 'string',
					'character_name' => 'string',
					'hull' => 'int',
					'engines' => 'int',
					'power' => 'int',
					'computer' => 'int',
					'sensors' => 'int',
					'beams' => 'int',
					'torp_launchers' => 'int',
					'torps' => 'int',
					'shields' => 'int',
					'armor' => 'int',
					'armor_pts' => 'int',
					'cloak' => 'int',
					'credits' => 'int',
					'sector' => 'int',
					'rating' => 'int',
					'ship_ore' => 'int',
					'ship_organics' => 'int',
					'ship_goods' => 'int',
					'ship_energy' => 'int',
					'ship_colonists' => 'int',
					'ship_fighters' => 'int',
					'turns' => 'int',
					'on_planet' => 'string',
					'dev_warpedit' => 'int',
					'dev_genesis' => 'int',
					'dev_beacon' => 'int',
					'dev_emerwarp' => 'int',
					'dev_escapepod' => 'string',
					'dev_fuelscoop' => 'string',
					'dev_minedeflector' => 'int',
					'last_login' => 'string',
					'trade_colonists' => 'string',
					'trade_fighters' => 'string',
					'trade_torps' => 'string',
					'trade_energy' => 'string',
					'dev_lssd' => 'string',
					'xenobe' => 'string',
				),
				array(
					$shipname,
					'N',
					$character,
					$xenlevel,
					$xenlevel,
					$xenlevel,
					$xenlevel,
					$xenlevel,
					$xenlevel,
					$xenlevel,
					$maxtorps,
					$xenlevel,
					$xenlevel,
					$maxarmor,
					$xen_cloak,
					$start_credits,
					$sector,
					$init_rating,
					0,
					0,
					0,
					$maxenergy,
					0,
					$maxfighters,
					$start_turns,
					'N',
					0,
					0,
					0,
					0,
					'N',
					'N',
					0,
					Now(),
					'N',
					'N',
					'N',
					'N',
					'N',
					'Y',
				),
				''
			);
			
			$message .= '<p>Xenobe created</p>';
			
			// Insert the new xenobe into the xenobe database
			$smcFunc['db_insert']('insert',
				$dbtables['xenobes'],
				array(
					'ship_id' => 'int',
					'active' => 'string',
					'aggression' => 'int',
					'orders' => 'int',
				),
				array(
					$smcFunc['db_insert_id']($dbtables['ships'], 'ship_id'),
					$active,
					$aggression,
					$orders,
				),
				''
			);
						
            // Xenobe has a 5% chance of getting a planet - rjordan01
            $getsplanet = rand(1,100);
			
            if ($getsplanet <= $xen_planets)
            {
                $message .= '<p>This Xenobe has been selected to get a planet</p>';
				
				$result = $smcFunc['db_query']('', '
					SELECT ship_id
					FROM {raw:db_ships}
					WHERE character_name = {string:character}
					LIMIT 1',
					array(
						'db_ships' => $dbtables['ships'],
						'character' => $character,
					)
				);
				$xenshipid = $smcFunc['db_fetch_assoc']($result);
				$smcFunc['db_free_result']($result);
                
				$result = $smcFunc['db_query']('', '
					SELECT planet_id
					FROM {raw:db_planets}
					WHERE owner = 0
					AND credits > 0
					ORDER BY credits DESC
					LIMIT 1',
					array(
						'db_planets' => $dbtables['planets'],
						'character' => $character,
					)
				);
				
				// If there are any empty planets, use one those, otherwise create a new one.
                if ($smcFunc['db_num_rows']($result) > 0)
				{
					$ep = $smcFunc['db_fetch_assoc']($result);
					$smcFunc['db_free_result']($result);
					
					$smcFunc['db_query']('', '
						UPDATE {raw:db_planets}
						SET organics = {int:fpf},
							ore = {int:fpf},
							goods = {int:fpf},
							energy = {int:fpf},
							colonists = {int:fpf},
							credits = {int:fpf},
							fighters = {int:fpf},
							torps = {int:fpf},
							owner = {int:ship_id},
							corp = "0",
							base = "Y",
							sells = "N",
							prod_organics = 15.00,
							prod_ore = 5.00,
							prod_goods = 5.00,
							prod_energy = 10.00,
							prod_fighters = 20.00,
							prod_torp = 10.00,
							defeated = "N"
						WHERE planet_id = {int:planet_id}',
						array(
							'db_planets' => $dbtables['planets'],
							'ship_id' => $xenshipid['ship_id'],
							'planet_id' => $ep['planet_id'],
							'fpf' => $fpf,
						)
					);
					
					$message .= '<p>Xenobe captured an abanded planet. Planet ID: ' . $ep['planet_id'] . '</p>';
                }
                else
                {						
                    // Check to see if a planet can be created in the current sector 
					$result = $smcFunc['db_query']('', '
						SELECT *
						FROM {raw:db_universe}
						WHERE sector_id = {int:sector}
						AND zone_id = 1',
						array(
							'db_universe' => $dbtables['universe'],
							'sector' => $sector,
						)
					);
					
                    if ($smcFunc['db_num_rows']($result) == 0)
                    {
                        $message .= '<p>Not allowed to create a planet in sector ' . $sector . '.</p>';
                    }
                    else
                    {
						$smcFunc['db_free_result']($result);
						
						$result = $smcFunc['db_query']('', '
							SELECT *
							FROM {raw:db_planets}
							WHERE sector_id = {int:sector}',
							array(
								'db_planets' => $dbtables['planets'],
								'sector' => $sector,
								
							)
						);
						
						// Make sure there is room for another planet
                        if ($smcFunc['db_num_rows']($result) >= $max_planets_sector)
                        {
                            $message .= '<p>There are too many planets in sector ' . $sector . '.</p>';
                        }
                        else
                        {								
                            $planet_number = $smcFunc['db_num_rows']($result) + 1;
                            $pname = 'Xenobe Dominion-' . $sector . '-' . $planet_number;
							$message .= '<p>The planet name is ' . $pname . '.</p>';
							
							$smcFunc['db_insert']('insert',
								$dbtables['planets'],
								array(
									'sector_id' => 'int',
									'name' => 'string',
									'organics' => 'int',
									'ore' => 'int',
									'goods' => 'int',
									'energy' => 'int',
									'colonists' => 'int',
									'credits' => 'int',
									'fighters' => 'int',
									'torps' => 'int',
									'owner' => 'int',
									'base' => 'string',
									'sells' => 'string',
									'prod_organics' => 'float',
									'prod_ore' => 'float',
									'prod_goods' => 'float',
									'prod_energy' => 'float',
									'prod_fighters' => 'float',
									'prod_torp' => 'float',
									'defeated' => 'string',
								),
								array(
									$sector,
									$pname,
									$fpf,
									$fpf,
									$fpf,
									$fpf,
									$fpf,
									$fpf,
									$fpf,
									$fpf,
									$xenshipid['ship_id'],
									'Y',
									'N',
									15.00,
									5.00,
									5.00,
									10.00,
									20.00,
									10.00,
									'N',
								),
								''
							);
							
							$message .= '<p>Xenobe Planet has been created.</p>';
						}
					}
				}
			}
		}
        $needed_xenobe--;
    }
}	

$xennonmove = $xencount - ($xencount0 + $xencount1 + $xencount2 + $xencount3);
$message .= '	<ul>
					<li>Counted ' . $xencount . ' Xenobe players that are ACTIVE with working ships.</li>
					<li>' . $xennonmove . ' Xenobe players did not do anything this round.</li>
					<li>' . $xencount0 . ' Xenobe players had SENTINEL orders of which ' . $xencount0a . ' launched attacks.</li>
					<li>' . $xencount1 . ' Xenobe players had ROAM orders of which ' . $xencount1a . ' launched attacks.</li>
					<li>' . $xencount2 . ' Xenobe players had ROAM AND TRADE orders of which ' . $xencount2a . ' launched attacks.</li>
					<li>' . $xencount3 . ' Xenobe players had ROAM AND HUNT orders of which '. $xencount3a . ' launched attacks and ' . $xencount3h . ' went hunting.</li>
				</ul>

<h3>XENOBE TURNS COMPLETE!</h3>';
?>
